package com.p595263720.o_amap.map.models;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import java.util.HashMap;
import java.util.List;

public class UnifiedMarkerOptions {
    /// Marker覆盖物的图标
    private String icon;
    /// Marker覆盖物的动画帧图标列表，动画的描点和大小以第一帧为准，建议图片大小保持一致
    private List<String> icons;
    /// Marker覆盖物的透明度
    private float alpha;
    /// Marker覆盖物锚点在水平范围的比例
    private float anchorU;
    /// Marker覆盖物锚点垂直范围的比例
    private float anchorV;
    /// Marker覆盖物是否可拖拽
    private boolean draggable;
    /// Marker覆盖物的InfoWindow是否允许显示, 可以通过 MarkerOptions.infoWindowEnable(kotlin.Booleanean) 进行设置
    public boolean infoWindowEnable;
    /// 设置多少帧刷新一次图片资源，Marker动画的间隔时间，值越小动画越快
    private int period;
    /// Marker覆盖物的位置坐标
    private LatLng position;
    /// Marker覆盖物的图片旋转角度，从正北开始，逆时针计算
    private float rotateAngle;
    /// Marker覆盖物是否平贴地图
    private boolean isFlat;
    /// Marker覆盖物的坐标是否是Gps，默认为false
    private boolean isGps;
    /// Marker覆盖物的水平偏移距离
    private int infoWindowOffsetX;
    /// Marker覆盖物的垂直偏移距离
    private int infoWindowOffsetY;
    /// 设置 Marker覆盖物的 文字描述
    public String snippet;
    /// Marker覆盖物 的标题
    public String title;
    /// Marker覆盖物是否可见
    private boolean visible;
    /// todo 缺少文档
    private boolean autoOverturnInfoWindow;
    /// Marker覆盖物 zIndex
    private float zIndex;
    /// 显示等级 缺少文档
    private int displayLevel;
    /// 是否在掩层下 缺少文档
    private boolean belowMaskLayer;
    // 自定义mark使用
    private byte[] widget;

    //额外参数
    public HashMap<String,Object> extData;

    public UnifiedMarkerOptions(MarkerOptions options, Marker marker) {
        alpha = options.getAlpha();
        anchorU = options.getAnchorU();
        anchorV = options.getAnchorV();
        draggable = options.isDraggable();
        infoWindowEnable = options.isInfoWindowEnable();
        period = options.getPeriod();
        position = options.getPosition();
        rotateAngle = options.getRotateAngle();
        isFlat = options.isFlat();
        infoWindowOffsetX = options.getInfoWindowOffsetX();
        infoWindowOffsetY = options.getInfoWindowOffsetY();
        snippet = options.getSnippet();
        title = options.getTitle();
        autoOverturnInfoWindow = options.isInfoWindowAutoOverturn();
        zIndex = options.getZIndex();
        displayLevel = options.getDisplayLevel();
        belowMaskLayer = options.isBelowMaskLayer();
        extData = (HashMap<String, Object>) marker.getObject();
    }

    public void applyTo(AMap map, Context context) {
        Marker marker = map.addMarker(toMarkerOption(context));
        if(extData!=null){
            marker.setObject(extData);
        }
        marker.showInfoWindow();
        map.moveCamera(CameraUpdateFactory.changeLatLng(position));
        //map.animateCamera(CameraUpdateFactory.newLatLngBounds(LatLngBounds.builder().include(position).build(), 100));
    }


    public MarkerOptions toMarkerOption(Context context) {
        BitmapDescriptor bitmapDescriptor = null;
        if (this.icon != null && this.widget == null) {
            bitmapDescriptor = UnifiedAssets.getBitmapDescriptor(this.icon,context);
        }
        if (this.icon == null && this.widget != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(widget, 0, widget.length);
            bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
        }

        return new MarkerOptions()
                .alpha(alpha)
                .anchor(anchorU, anchorV)
                .draggable(draggable)
                .infoWindowEnable(infoWindowEnable)
                .period(period)
                .position(position)
                .rotateAngle(rotateAngle)
                .setFlat(isFlat)
                .setGps(isGps)
                .setInfoWindowOffset(infoWindowOffsetX, infoWindowOffsetY)
                .snippet(snippet)
                .title(title)
                .visible(visible)
                .autoOverturnInfoWindow(autoOverturnInfoWindow)
                .zIndex(zIndex)
                .displayLevel(displayLevel)
                .belowMaskLayer(belowMaskLayer)
                .icon(bitmapDescriptor);
    }

}
